package com.df.springmvc.system.manage.controller;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.df.springmvc.common.bean.CurrentUserBean;
import com.df.springmvc.common.bean.MsgBean;
import com.df.springmvc.common.util.WebUtil;
import com.df.springmvc.supper.SupperController;
import com.df.springmvc.system.auth.services.AuthServices;
import com.df.springmvc.system.manage.services.LoginServices;
import com.df.springmvc.system.role.services.RoleServices;


@Controller
@RequestMapping("/system/manage/Login")
/**
 * 本类仅做一些简单数据处理如:
 *格式验证
 *数据封装
 * @author David
 *
 */
public class LoginController extends SupperController{
	String page_model="system/manage/" + this.getClass().getSimpleName().replace("Controller","") + "_";
	@Autowired
	LoginServices loginServices;
	@Autowired
	MessageSource messageSource;
	@Autowired
	RoleServices roleServices;
	@Autowired
	AuthServices authServices;
	public LoginController(){
		// TODO Auto-generated constructor stub
	}
	@RequestMapping(value={"/"},method=RequestMethod.GET)
	public String login(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		return page_model;
	}
	/**
	 * 用户进行登录
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping(value={"/doLogin"})
	public void doLogin(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		String u=getParam(request,"u");
		String c=getParam(request,"c");
		//String c2="" + getSession().getAttribute("code");
		String c2=getParam(request,"c2");
		String p=getParam(request,"p");
		try{
			if(!c2.equalsIgnoreCase(c)){
				throw new Exception("验证码不正确");
			}
			//String userName=loginServices.vali(u,p);
			 ArrayList<String> list=loginServices.vali2(u,p);
			 if(list.size()>0){
				 String userName =list.get(0);
				 String userAccount =list.get(1);
				 WebUtil.json(response,new MsgBean("用户名与密码正确",true));
					CurrentUserBean currentUserBean=new CurrentUserBean();
					currentUserBean.setFullname(userName);
					currentUserBean.setUserId(userAccount);
					currentUserBean.setLoginName(userAccount);
					currentUserBean.setLoginPassword(p);
					request.getSession().setAttribute("CurrentUserBean",currentUserBean);
			 }else{
				 throw new Exception("登录失败，请重新登录！");
			 }
			
			
			
		}catch(Exception e){
			// TODO: handle exception
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
		}finally{
		}
	}
	@Override
	@RequestMapping("/checkLogin")
	public void checkLogin(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		String userId=getCurrentUserBean().getUserId();
		if(userId == null){
			WebUtil.json(response,new MsgBean("err",false));
		}else{
			WebUtil.json(response,new MsgBean("ok",true));
		}
		super.doConfig(model,request,response);
	}
	/**
	 * 密码找回
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value={"/toGetPassword"})
	public String toGetPassword(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		try{
			//addRequestAttribute("map",services.view(getParamMap(request,"map")));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return page_model + "GetPassword";
	}
	/**
	 * 发送手机验证码
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping(value={"/doSendSMS"})
	public void doSendSMS(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		//WebUtil.json(response,new MsgBean("OK"));
		String telphone = getParamMap(request,"map").get("telphone");
		try{
			HashMap<String,String> rs = loginServices.sendSMS(getParamMap(request,"map"));
			request.getSession().setAttribute(telphone,rs.get(telphone));
			WebUtil.json(response,new MsgBean(rs.get("result")));
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 执行修改密码
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping(value={"/doRetrievePassword"})
	public void doRetrievePassword(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		String telphone = getParamMap(request,"map").get("telphone");
		String code = getParamMap(request,"map").get("VerificationCode"); 
		String VerCode=""+request.getSession().getAttribute(telphone);
		try{
			if(!code.equalsIgnoreCase(VerCode)){
				throw new Exception("验证码不正确！");
			}
			Object rs=loginServices.retrievePassword(getParamMap(request,"map"));
			CurrentUserBean currentUserBean=new CurrentUserBean();
			currentUserBean.setUserId(getParamMap(request,"map").get("userAccount"));
			currentUserBean.setLoginName(getParamMap(request,"map").get("userAccount"));
			currentUserBean.setLoginPassword(getParamMap(request,"map").get("password"));
			request.getSession().setAttribute("CurrentUserBean",currentUserBean);
			WebUtil.json(response,new MsgBean(rs));
		}catch(Exception e){
			// TODO Auto-generated catch block
			WebUtil.json(response,new MsgBean(e.getMessage(),false));
			e.printStackTrace();
		}
		
		//WebUtil.json(response,new MsgBean("OK"));
	}
}
